package com.xindz.cn.mvptest.components.product.model;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.GetCallback;
import com.avos.avoscloud.SaveCallback;
import com.xindz.cn.mvptest.components.product.data.AttachProductBean;
import com.xindz.cn.mvptest.components.product.data.ProductBean;

/**
 * @name MvpTest
 * @class name：com.xindz.cn.mvptest.components.product.model
 * @describe 产品的请求
 * @anthor XINDZ
 * @time 2017/10/30 15:56
 * @change
 * @chang time
 * @class describe
 */
public class ProductModel {
    /**
     * 获取
     * @param objectID
     * @param callback
     */
    public void getInBackground(String objectID, GetCallback<ProductBean> callback) {
        AVQuery avQuery = new AVQuery("Product");
        avQuery.include("AttachProduct");
        avQuery.getInBackground(objectID, callback);

    }

    /**
     * 查找
     * @param callback
     */
    public void findInBackground(FindCallback<ProductBean> callback) {
        AVQuery avQuery = new AVQuery("Product");
        avQuery.include("owner");
        avQuery.limit(2);
        avQuery.orderByDescending("createdAt");
        avQuery.findInBackground(callback);

    }

    /**
     * 保存
     * @param attachProductBean
     * @param title
     * @param des
     * @param imageUrl
     * @param xindz
     * @param price
     * @param callback
     */
    public void saveInBackground(AttachProductBean attachProductBean, String title, String des, String imageUrl, String xindz, int price, SaveCallback callback) {
        ProductBean productBean = new ProductBean();
        productBean.put("AttachProduct", attachProductBean);
        productBean.setTitle(title);
        productBean.setDescription(des);
        productBean.setImage(imageUrl, "http://ww3.sinaimg.cn/large/7a8aed7bgw1eswencfur6j20hq0qodhs.jpg");
        productBean.setXindz(xindz);
        productBean.setPrice(price);
        productBean.saveInBackground(callback);
    }

    /**
     *
     * @param avObject
     */
    public void saveInBackground(AVObject avObject) {
        avObject.saveInBackground();
    }

    /**
     * 指定刷新的 key 字符串
     * @param keys
     * @param avObject
     * @param callback
     */
    public void fetchInBackground(String keys ,AVObject avObject, GetCallback<AVObject> callback) {
        avObject.fetchInBackground(keys, callback);

}

    /**
     * 删除
     * @param avObject
     */
    public void deleteInBackground(AVObject avObject) {
        avObject.deleteInBackground();
    }

}
